/*
 * Author: xiguan
 * Mail: xiguan.teng@qq.com
 * -----
 * Last Modified: 2022-05-27 14:52:40
 * Modified By: xiguan
 * -----
 * Copyright (c) 2022 XiGuan Inc.
 * 
 * No Bug No error No warning.
 */



#include <bits/stdc++.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>


using namespace std;

void message_err(const char *msg){
    perror(msg);
    exit(1);
}



int main(int argc, char *argv[]){

    int fd1;
    int ret;
    int access_mode;

    if(argc < 2){
        printf("Usage: %s <file>\n", argv[0]);
        exit(1);
    }

    if((fd1 = open(argv[1], O_RDWR)) == -1){
        message_err("open: ");
    }
    
    // 设置文件的打开方式
    if((ret = fcntl(fd1, F_SETFL, O_APPEND)) < 0){
        message_err("fcntl: ");
    }
    // 获取文件打开的方式
    if((ret = fcntl(fd1, F_GETFL, 0)) < 0){
        message_err("fcntl: ");
    }
    
    access_mode = ret & O_ACCMODE;

    if(access_mode == O_WRONLY){
        cout << "write only." << endl;
    } else if(access_mode == O_RDONLY){
        cout << "read only." << endl;
    } else if(access_mode == O_RDWR){
        cout << "read + write." << endl;
    }

    if(ret & O_APPEND){
        cout << "append." << endl;
    }
    if(ret & O_NONBLOCK){
        cout << "nonblock." << endl;
    }
    if(ret & O_ASYNC){
        cout << "ASYNC." << endl;
    }

    return 0;
}